//
//  CustomerSearchViewController.m
//  QRMaster
//
//  Created by liuyunlong on 15/10/24.
//  Copyright © 2015年 myApple. All rights reserved.
//

#import "CustomerSearchViewController.h"

#import "RequestNetWork.h"
#import "PromptInfoView.h"
#import "Reachability.h"
#import "UIImageView+WebCache.h"
#import "MoreCustomerViewController.h"
#import "GuestManagerViewController.h"
#import "groupInfoViewController.h"
#import "customSearchTableViewCell.h"
#import "customSearchModel.h"

@interface CustomerSearchViewController ()
{
    UITableView * backTabelView;
    NSArray * backChooseArray;
    NSArray * backChooseImgArray;
    UIImageView * allBackImgView;//点击加号是遮住整个view的透明的遮罩层
    CGRect backTabelViewFrame;
    
    UILabel * communityNameLabel;//集群
    UILabel * allNameLabel;//集群到房间的名字
    UIView * nameLabelView;
    UIActivityIndicatorView * _activityIndicator;
    NSInteger communityManager;
    
    //多语言
    UILabel * cancelSearchLabel;
    UILabel * promtLable;
    UILabel * promtGroupManagerLable;
    UILabel * netWorkBreakLabel;
    UILabel * promtLabel;
    NSArray * nameArray;
    
    UIView *titleView;
    UILabel *titleNameLabel;
    
    UISearchBar *nameSearchBar;
    
    UIImageView *searchImgView;
    
    UIImageView *cancelImgView;
    
    UIView *NetWorkBreakView;
    
    UITableView *searchResultTableView;
    
    NSString *communityIdStr;
    
    NSMutableDictionary *dataDic;//所有数据dic
    NSMutableArray *groupArray;  //组数组
    NSMutableArray *normalArray;//普通数组
    
    NSMutableDictionary *groupPageDic;  //组页
    NSMutableDictionary *normalPageDic;//普通页
    

    NSString *customStatus;             //客户状态，0表示组客户，1表示普通客户
    NSString *searchKey;                //搜索关键字
    
    
    NSString *groupIdStr;             //组id
    NSString *clientIdStr;            //
    NSString *memberIdsStr;           //
    
    UIButton *cancelSearchBtn;        //取消按钮
}
@end

@implementation CustomerSearchViewController

-(id)initWIthCommunityId:(NSString *)communityId
{
    if (self==[super init])
    {
        communityIdStr=communityId;
    }
    return self;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=UIColorFromRGB(0xebebeb);
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
    [self prepareTitleView];
    dataDic=[[NSMutableDictionary alloc]init];
    groupArray=[[NSMutableArray alloc]init];
    normalArray=[[NSMutableArray alloc]init];
    groupPageDic=[[NSMutableDictionary alloc]init];
    normalPageDic=[[NSMutableDictionary alloc]init];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    nameSearchBar.hidden = NO;
    cancelSearchBtn.hidden = NO;
    nameSearchBar.placeholder = NSLocalizedString(@"managerMentSearchContent", nil);
    [cancelSearchBtn setTitle:NSLocalizedString(@"alterCancel", nil) forState:0];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    nameSearchBar.hidden = YES;
    cancelSearchBtn.hidden = YES;
    [nameSearchBar resignFirstResponder];
       nameSearchBar.frame = CGRectMake(50, 0, [UIScreen mainScreen].bounds.size.width - 70-40, 44);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)prepareTitleView
{
    nameSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH - 70, 44)];
    nameSearchBar.hidden = NO;
    nameSearchBar.delegate = self;
    nameSearchBar.placeholder = NSLocalizedString(@"managerMentSearchContent", nil);
    [nameSearchBar becomeFirstResponder];
    nameSearchBar.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar addSubview:nameSearchBar];
    
    
    
    cancelSearchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelSearchBtn.frame=CGRectMake(UI_SCREEN_WIDTH - 60, 0, 60, 44);
    [cancelSearchBtn setTitle:NSLocalizedString(@"alterCancel", nil) forState:0];
    [cancelSearchBtn addTarget:self action:@selector(cancelSearchClick) forControlEvents:UIControlEventTouchUpInside];
    cancelSearchBtn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    cancelSearchBtn.hidden = YES;
    [self.navigationController.navigationBar addSubview:cancelSearchBtn];
    
    
    searchResultTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    searchResultTableView.delegate=self;
    searchResultTableView.dataSource=self;
    searchResultTableView.backgroundColor = UIColorFromRGB(0xebebeb);
    [self setExtraCellLineHidden:searchResultTableView];
    [self.view addSubview:searchResultTableView];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
    [self getCustomerInfo:communityIdStr search:nameSearchBar.text currentPage:nil size:nil status:@"0,1"];
    
}

-(void)cancelSearchClick
{
    [nameSearchBar resignFirstResponder];
}


-(void)getCustomerInfo:(NSString *)communityId search:(NSString *)search currentPage:(NSString *)currentPage size:(NSString *)size status:(NSString *)status
{
    if ([self IsHaveNetwork]) {
        return;
    }
    if(search==nil || [@"" isEqualToString:search]==YES)
    {
        [PromptInfoView showMessage:NSLocalizedString(@"SearchError", nil)];
        return;
    }
    [_activityIndicator startAnimating];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:communityId forKey:@"communityId"];
    [postDict setValue:search forKey:@"search"];
    [postDict setValue:currentPage forKey:@"currentPage"];
    [postDict setValue:size forKey:@"size"];
    [postDict setValue:status forKey:@"status"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(SEARCHCUSTOMER) params:postDict block:^(id downLoadData, id error)
     {
         if ([downLoadData objectForKey:@"data"]!=nil) {
             [groupArray removeAllObjects];
             [normalArray removeAllObjects];
             if([[downLoadData objectForKey:@"data"] objectForKey:@"group"]!=nil)
             {
                 NSArray * groupCustomArray = [[[downLoadData objectForKey:@"data"] objectForKey:@"group"] objectForKey:@"customers"];
                 groupPageDic = [[[downLoadData objectForKey:@"data"] objectForKey:@"group"]objectForKey:@"page"];
                 for (NSDictionary * dict in groupCustomArray) {
                     customSearchModel * model = [[customSearchModel alloc]init];
                     model.email = [dict objectForKey:@"email"];
                     model.firstChar = [dict objectForKey:@"firstchar"];
                     model.groupId = [dict objectForKey:@"groupid"];
                     model.gender = [dict objectForKey:@"gender"];
                     model.groupName = [dict objectForKey:@"groupname"];
                     model.headurl = [dict objectForKey:@"headurl"];
                     model.identity =[dict objectForKey:@"identity"];
                     model.mobile = [dict objectForKey:@"mobile"];
                     model.userId = [dict objectForKey:@"id"];
                     model.name = [dict objectForKey:@"name"];
                     model.areaCode = [dict objectForKey:@"areaCode"];
                     [groupArray addObject:model];
                 }
                 
                 for(int i = 0; i<groupArray.count; i++)
                 {
                     ((customSearchModel*)[groupArray objectAtIndex:i]).firstChar=[self FirstCharToStr:((customSearchModel *)[groupArray objectAtIndex:i]).firstChar];
                 }
                 //首字母处理
                 for(int i = 0; i<groupArray.count; i++)
                 {
                     for(int j = i; j<groupArray.count; j++)
                     {
                         if([((customSearchModel *)[groupArray objectAtIndex:i]).firstChar isEqualToString:((customSearchModel *)[groupArray objectAtIndex:j]).firstChar]==YES &&j!=i)
                         {
                             customSearchModel * model = [[customSearchModel alloc]init];
                             model=[groupArray objectAtIndex:j];
                             model.firstChar=@"";
                         }
                     }
                 }
                 
             }
             if([[downLoadData objectForKey:@"data"] objectForKey:@"normal"]!=nil)
             {
                 NSArray * normalCustomArray = [[[downLoadData objectForKey:@"data"] objectForKey:@"normal"] objectForKey:@"customers"];
                 normalPageDic = [[[downLoadData objectForKey:@"data"] objectForKey:@"normal"]objectForKey:@"page"];
                 for (NSDictionary * dict in normalCustomArray) {
                     customSearchModel * model = [[customSearchModel alloc]init];
                     model.email = [dict objectForKey:@"email"];
                     model.firstChar = [dict objectForKey:@"firstchar"];
                     model.gender = [dict objectForKey:@"gender"];
                     model.headurl = [dict objectForKey:@"headurl"];
                     model.identity =[dict objectForKey:@"identity"];
                     model.mobile = [dict objectForKey:@"mobile"];
                     model.userId = [dict objectForKey:@"id"];
                     model.name = [dict objectForKey:@"name"];
                     model.areaCode = [dict objectForKey:@"areaCode"];
                     [normalArray addObject:model];
                 }
                 for(int i = 0; i<normalArray.count; i++)
                 {
                     ((customSearchModel*)[normalArray objectAtIndex:i]).firstChar=[self FirstCharToStr:((customSearchModel *)[normalArray objectAtIndex:i]).firstChar];
                 }
                 
                 //首字母处理
                 for(int i = 0; i<normalArray.count; i++)
                 {
                     for(int j = i; j<normalArray.count; j++)
                     {
                         if([((customSearchModel *)[normalArray objectAtIndex:i]).firstChar isEqualToString:((customSearchModel *)[normalArray objectAtIndex:j]).firstChar]==YES &&j!=i)
                         {
                             customSearchModel * model = [[customSearchModel alloc]init];
                             model=[normalArray objectAtIndex:j];
                             model.firstChar=@"";
                         }
                     }
                 }
             }
             
             if (groupArray.count == 0 && normalArray.count == 0) {
                 [PromptInfoView showMessage:NSLocalizedString(@"SearchEmpty", nil)];
             }
         }
         else
         {
             [PromptInfoView showMessage:[error objectForKey:@"info"]];
         }
         [searchResultTableView reloadData];
         [_activityIndicator stopAnimating];
     }
     ];
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
     cancelSearchBtn.hidden = NO;
     nameSearchBar.frame=CGRectMake(50, 0, [UIScreen mainScreen].bounds.size.width - 70-40, 44);
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    nameSearchBar.frame=CGRectMake(50, 0, UI_SCREEN_WIDTH - 60, 44);
    cancelSearchBtn.hidden = YES;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    int num=0;
    if (groupArray.count!=0) {
        num+=1;
    }
    if (normalArray.count!=0) {
        num+=1;
    }
    return num;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger num=0;
    if(groupArray.count!=0 && normalArray.count!=0)
    {
        if (section==0) {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                num=groupArray.count+1;
            }
            else
            {
                num=groupArray.count;
            }
        }
        else if (section==1) {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                num=normalArray.count+1;
            }
            else
            {
                num=normalArray.count;
            }
            
        }
    }
    else if(groupArray.count!=0 && normalArray.count==0)
    {
        if (section==0) {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                num=groupArray.count+1;
            }
            else
            {
                num=groupArray.count;
            }
        }
    }
    else if(groupArray.count==0 && normalArray.count!=0)
    {
        if (section==0) {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                num=normalArray.count+1;
            }
            else
            {
                num=normalArray.count;
            }
            
        }
    }
    return num;
}

//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    NSString *titleStr;
//    if(groupArray.count!=0 && normalArray.count!=0)
//    {
//        if (section==0) {
//            titleStr=NSLocalizedString(@"groupGuest", nil);
//        }
//        else
//        {
//            titleStr=NSLocalizedString(@"normalGuest", nil);
//        }
//    }
//    else if(groupArray.count!=0 && normalArray.count==0)
//    {
//        if (section==0) {
//            titleStr=NSLocalizedString(@"groupGuest", nil);
//        }
//    }
//    else if(groupArray.count==0 && normalArray.count!=0)
//    {
//        if (section==0) {
//            titleStr=NSLocalizedString(@"normalGuest", nil);
//        }
//    }
//    return titleStr;
//    
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 39)];
        customView.backgroundColor = UIColorFromRGB(0xebebeb);
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 14, 100, 17)];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = UIColorFromRGB(0x333333);
        [customView addSubview:label];
        if(groupArray.count!=0 && normalArray.count!=0)
        {
                label.text = NSLocalizedString(@"groupGuest", nil);
        }
        else if(groupArray.count!=0 && normalArray.count==0)
        {
                label.text = NSLocalizedString(@"groupGuest", nil);
        }
        else if(groupArray.count==0 && normalArray.count!=0)
        {
                label.text = NSLocalizedString(@"normalGuest", nil);
        }
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 38.5, UI_SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = RGBA(193, 193, 193, 1);
        [customView addSubview:lineView];
        return customView;
    }else{
        
        UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 34)];
        customView.backgroundColor = UIColorFromRGB(0xebebeb);
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 9, 100, 17)];
        label.font = [UIFont systemFontOfSize:15];
        label.textColor = UIColorFromRGB(0x333333);
        [customView addSubview:label];
        if(groupArray.count!=0 && normalArray.count!=0)
        {
            label.text = NSLocalizedString(@"normalGuest", nil);
        }
        UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 33.5, UI_SCREEN_WIDTH, 0.5)];
        lineView.backgroundColor = RGBA(193, 193, 193, 1);
        [customView addSubview:lineView];
        return customView;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 0.5)];
    footerView.backgroundColor = RGBA(193, 193, 193, 1);
    return footerView;

}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 39;
    }else{
        return 34;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.5;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * cellId = @"cell";
    customSearchTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[customSearchTableViewCell alloc]init];
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if(groupArray.count!=0 && normalArray.count!=0)
    {
        if(indexPath.section==0)
        {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if (indexPath.row == groupArray.count) {
                    cell.customName = nil;
//                    cell.firstChar = nil;
                    cell.customMobile = nil;
//                    cell.customHeadImg = nil;
//                    cell.textLabel.text=NSLocalizedString(@"more", nil);
                    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 16)];
                    label.font = [UIFont systemFontOfSize:15];
                    label.textColor = UIColorFromRGB(0x333333);
                    label.text = @"查看更多";
                    [cell.contentView addSubview:label];
                }
                else if(groupArray.count > indexPath.row)
                {
                    customSearchModel * model = groupArray[indexPath.row];
                    cell.customName = model.name;
//                    cell.firstChar = model.firstChar;
                    if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                        if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                            cell.customMobile = model.mobile;
                        }
                        else
                        {
                            cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                        }
                    }
                    else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
                    {
                        cell.customMobile = model.email;
                    }

                    cell.customHeadImg = model.headurl;
                }
            }
            else if(groupArray.count > indexPath.row)
            {
                customSearchModel * model = groupArray[indexPath.row];
                cell.customName = model.name;
//                cell.firstChar = model.firstChar;
                if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                    if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                        cell.customMobile = model.mobile;
                    }
                    else
                    {
                        cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                    }
                }
                else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
                {
                    cell.customMobile = model.email;
                }


                cell.customHeadImg = model.headurl;
            }
        }
        if(indexPath.section==1)
        {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if (indexPath.row ==normalArray.count) {
                    cell.customName = nil;
//                    cell.firstChar = nil;
                    cell.customMobile = nil;
//                    cell.customHeadImg = nil;
//                    cell.textLabel.text=NSLocalizedString(@"more", nil);
                    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 16)];
                    label.font = [UIFont systemFontOfSize:15];
                    label.textColor = UIColorFromRGB(0x333333);
                    label.text = @"查看更多";
                    [cell.contentView addSubview:label];
                }
                else if(normalArray.count > indexPath.row)
                {
                    customSearchModel * model = normalArray[indexPath.row];
                    cell.customName = model.name;
//                    cell.firstChar = model.firstChar;
                    if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                        if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                            cell.customMobile = model.mobile;
                        }
                        else
                        {
                            cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                        }
                    }
                    else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
                    {
                        cell.customMobile = model.email;
                    }


                    cell.customHeadImg = model.headurl;
                }
                
            }
            else if(normalArray.count > indexPath.row)
            {
                customSearchModel * model = normalArray[indexPath.row];
                cell.customName = model.name;
//                cell.firstChar = model.firstChar;
                cell.customMobile = model.mobile;
                cell.customHeadImg = model.headurl;
            }
        }
    }
    else if(groupArray.count!=0 && normalArray.count==0)
    {
        if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
        {
            if (indexPath.row  == groupArray.count) {
                cell.customName = nil;
//                cell.firstChar = nil;
                cell.customMobile = nil;
//                cell.customHeadImg = nil;
//                cell.textLabel.text=NSLocalizedString(@"more", nil);
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 16)];
                label.font = [UIFont systemFontOfSize:15];
                label.textColor = UIColorFromRGB(0x333333);
                label.text = @"查看更多";
                [cell.contentView addSubview:label];
            }
            else if(groupArray.count > indexPath.row)
            {
                customSearchModel * model = groupArray[indexPath.row];
                cell.customName = model.name;
//                cell.firstChar = model.firstChar;
                if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                    if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                        cell.customMobile = model.mobile;
                    }
                    else
                    {
                        cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                    }
                }
                else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
                {
                    cell.customMobile = model.email;
                }

                cell.customHeadImg = model.headurl;
            }
        }
        else if(groupArray.count > indexPath.row)
        {
            customSearchModel * model = groupArray[indexPath.row];
            cell.customName = model.name;
//            cell.firstChar = model.firstChar;
            if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                    cell.customMobile = model.mobile;
                }
                else
                {
                    cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                }
            }
            else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
            {
                cell.customMobile = model.email;
            }

            cell.customHeadImg = model.headurl;
        }
    }
    else if(groupArray.count==0 && normalArray.count!=0 )
    {
        if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
        {
            if (indexPath.row == normalArray.count) {
                cell.customName = nil;
//                cell.firstChar = nil;
                cell.customMobile = nil;
//                cell.customHeadImg = nil;
//                cell.textLabel.text=NSLocalizedString(@"more", nil);
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, 100, 16)];
                label.font = [UIFont systemFontOfSize:15];
                label.textColor = UIColorFromRGB(0x333333);
                label.text = @"查看更多";
                [cell.contentView addSubview:label];
               
            }
            else if(normalArray.count > indexPath.row)
            {
                customSearchModel * model = normalArray[indexPath.row];
                cell.customName = model.name;
//                cell.firstChar = model.firstChar;
                if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                    if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                        cell.customMobile = model.mobile;
                    }
                    else
                    {
                        cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                    }
                }
                else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
                {
                    cell.customMobile = model.email;
                }
                cell.customHeadImg = model.headurl;
            }
        }
        else if(normalArray.count > indexPath.row)
        {
            customSearchModel * model = normalArray[indexPath.row];
            cell.customName = model.name;
//            cell.firstChar = model.firstChar;
            if (model.mobile!=nil && [@"" isEqualToString:model.mobile]==NO) {
                if ([model.areaCode isEqualToString:[QRSingleton sharedQRSingleton].currentAreaCodeString]) {
                    cell.customMobile = model.mobile;
                }
                else
                {
                    cell.customMobile = [NSString stringWithFormat:@"+%@ %@",model.areaCode,model.mobile];
                }
            }
            else if (model.email!=nil && [@"" isEqualToString:model.email]==NO)
            {
                cell.customMobile = model.email;
            }

            cell.customHeadImg = model.headurl;
        }
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(groupArray.count!=0 && normalArray.count!=0)
    {
        if (indexPath.section==0) {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== groupArray.count)
                {
                    //跳转到更多客户信息页面
                    if (nameSearchBar.text!=nil && nameSearchBar.text.length>0) {
                        MoreCustomerViewController *more=[[MoreCustomerViewController alloc]initWIthCustomerStatus:@"1" searchKey:nameSearchBar.text communityId:communityIdStr];
                        [self.navigationController pushViewController:more animated:YES];
                    }
                    else
                    {
                        [PromptInfoView showMessage:NSLocalizedString(@"SearchKey", nil)];
                        
                    }
                    return;
                }
                else if(indexPath.row < groupArray.count)
                {
                    GuestModel * gModel = [[GuestModel alloc]init];
                    customSearchModel * model = groupArray[indexPath.row];
                    gModel.guestName = model.name;
                    gModel.sex = model.gender;
                    gModel.email = model.email;
                    gModel.telePhone = model.mobile;
                    gModel.guestid = model.userId;
                    gModel.isGroupManager = @"0";
                    gModel.certificaties = model.identity;
                    gModel.areaCode = model.areaCode;
                    groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                    gifv.groupId = model.groupId;
                    gifv.groupName = model.groupName;
                    gifv.model = gModel;
                    gifv.communityId = communityIdStr;
                    [self.navigationController pushViewController:gifv animated:YES];
                }
            }
            else if(indexPath.row < groupArray.count)
            {
                GuestModel * gModel = [[GuestModel alloc]init];
                customSearchModel * model = groupArray[indexPath.row];
                gModel.guestName = model.name;
                gModel.sex = model.gender;
                gModel.email = model.email;
                gModel.telePhone = model.mobile;
                gModel.guestid = model.userId;
                gModel.isGroupManager = @"0";
                gModel.certificaties = model.identity;
                gModel.areaCode = model.areaCode;
                groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                gifv.groupId = model.groupId;
                gifv.groupName = model.groupName;
                gifv.model = gModel;
                gifv.communityId = communityIdStr;
                [self.navigationController pushViewController:gifv animated:YES];
                
            }
        }
        else if (indexPath.section==1) {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== normalArray.count)
                {
                    //跳转到更多客户信息页面
                    if (nameSearchBar.text!=nil && nameSearchBar.text.length>0) {
                        MoreCustomerViewController *more=[[MoreCustomerViewController alloc]initWIthCustomerStatus:@"0" searchKey:nameSearchBar.text communityId:communityIdStr];
                        [self.navigationController pushViewController:more animated:YES];
                    }
                    else
                    {
                        [PromptInfoView showMessage:NSLocalizedString(@"SearchKey", nil)];
                    }
                    return;
                }
                else if(indexPath.row < normalArray.count)
                {
                    GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                    GuestModel *model=[[GuestModel alloc]init];
                    customSearchModel * cModel = normalArray[indexPath.row];
                    model.guestName=cModel.name;
                    model.telePhone=cModel.mobile;
                    model.email=cModel.email;
                    model.guestid=cModel.userId;
                    model.headUrl=cModel.headurl;
                    model.sex=cModel.gender;
                    model.certificaties=cModel.identity;
                    model.firstChar=cModel.firstChar;
                    model.areaCode = cModel.areaCode;
                    gmvc.model = model;
                    gmvc.communityid = communityIdStr;
                    gmvc.comeFrom = NO;
                    [QRSingleton sharedQRSingleton].guestPushIndex = 2;
                    [self.navigationController pushViewController:gmvc animated:YES];
                }
            }
            else if(indexPath.row < normalArray.count)
            {
                GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                GuestModel *model=[[GuestModel alloc]init];
                customSearchModel * cModel = normalArray[indexPath.row];
                model.guestName=cModel.name;
                model.telePhone=cModel.mobile;
                model.email=cModel.email;
                model.guestid=cModel.userId;
                model.headUrl=cModel.headurl;
                model.sex=cModel.gender;
                model.certificaties=cModel.identity;
                model.firstChar=cModel.firstChar;
                model.areaCode = cModel.areaCode;
                [QRSingleton sharedQRSingleton].guestPushIndex = 2;
                gmvc.model = model;
                gmvc.communityid = communityIdStr;
                gmvc.comeFrom = NO;
                [self.navigationController pushViewController:gmvc animated:YES];
            }
        }
    }
    else if(groupArray.count!=0 && normalArray.count==0)
    {
        if (indexPath.section==0) {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== groupArray.count)
                {
                    //跳转到更多客户信息页面
                    if (nameSearchBar.text!=nil && nameSearchBar.text.length>0) {
                        MoreCustomerViewController *more=[[MoreCustomerViewController alloc]initWIthCustomerStatus:@"1" searchKey:nameSearchBar.text communityId:communityIdStr];
                        [self.navigationController pushViewController:more animated:YES];
                    }
                    else
                    {
                        [PromptInfoView showMessage:NSLocalizedString(@"SearchKey", nil)];
                    }
                    return;
                }
                else if(indexPath.row < groupArray.count)
                {
                    GuestModel * gModel = [[GuestModel alloc]init];
                    customSearchModel * model = groupArray[indexPath.row];
                    gModel.guestName = model.name;
                    gModel.sex = model.gender;
                    gModel.email = model.email;
                    gModel.telePhone = model.mobile;
                    gModel.isGroupManager = @"0";
                    gModel.guestid = model.userId;
                    gModel.certificaties = model.identity;
                    gModel.areaCode = model.areaCode;
                    groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                    gifv.groupId = model.groupId;
                    gifv.groupName = model.groupName;
                    gifv.model = gModel;
                    gifv.communityId = communityIdStr;
                    [self.navigationController pushViewController:gifv animated:YES];
                }
            }
            else if(indexPath.row < groupArray.count)
            {
                GuestModel * gModel = [[GuestModel alloc]init];
                customSearchModel * model = groupArray[indexPath.row];
                gModel.guestName = model.name;
                gModel.sex = model.gender;
                gModel.email = model.email;
                gModel.telePhone = model.mobile;
                gModel.guestid = model.userId;
                gModel.isGroupManager = @"0";
                gModel.certificaties = model.identity;
                gModel.areaCode = model.areaCode;
                groupInfoViewController * gifv = [[groupInfoViewController alloc]init];
                gifv.groupId = model.groupId;
                gifv.groupName = model.groupName;
                gifv.model = gModel;
                gifv.communityId = communityIdStr;
                [self.navigationController pushViewController:gifv animated:YES];
                
            }
        }
        else if(groupArray.count==0 && normalArray.count!=0)
        {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== normalArray.count)
                {
                    //跳转到更多客户信息页面
                    if (nameSearchBar.text!=nil && nameSearchBar.text.length>0) {
                        MoreCustomerViewController *more=[[MoreCustomerViewController alloc]initWIthCustomerStatus:@"1" searchKey:nameSearchBar.text communityId:communityIdStr];
                        [self.navigationController pushViewController:more animated:YES];
                    }
                    else
                    {
                        [PromptInfoView showMessage:NSLocalizedString(@"SearchKey", nil)];
                    }
                    
                }
                else if(indexPath.row < normalArray.count)
                {
                    GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                    GuestModel *model=[[GuestModel alloc]init];
                    customSearchModel * cModel = normalArray[indexPath.row];
                    model.guestName=cModel.name;
                    model.telePhone=cModel.mobile;
                    model.email=cModel.email;
                    model.guestid=cModel.userId;
                    model.headUrl=cModel.headurl;
                    model.sex=cModel.gender;
                    model.certificaties=cModel.identity;
                    model.firstChar=cModel.firstChar;
                    model.areaCode = cModel.areaCode;
                    [QRSingleton sharedQRSingleton].guestPushIndex = 2;
                    gmvc.model = model;
                    gmvc.communityid = communityIdStr;
                    gmvc.comeFrom = NO;
                    [self.navigationController pushViewController:gmvc animated:YES];
                }
            }
            else if(indexPath.row < normalArray.count)
            {
                GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                GuestModel *model=[[GuestModel alloc]init];
                customSearchModel * cModel = normalArray[indexPath.row];
                model.guestName=cModel.name;
                model.telePhone=cModel.mobile;
                model.email=cModel.email;
                model.guestid=cModel.userId;
                model.headUrl=cModel.headurl;
                model.sex=cModel.gender;
                model.certificaties=cModel.identity;
                model.firstChar=cModel.firstChar;
                model.areaCode = cModel.areaCode;
                [QRSingleton sharedQRSingleton].guestPushIndex = 2;
                gmvc.model = model;
                gmvc.communityid = communityIdStr;
                gmvc.comeFrom = NO;
                [self.navigationController pushViewController:gmvc animated:YES];
            }
        }
    }
    else if(groupArray.count==0 && normalArray.count!=0)
    {
        if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
        {
            if(indexPath.row== normalArray.count)
            {
                //跳转到更多客户信息页面
                if (nameSearchBar.text!=nil && nameSearchBar.text.length>0) {
                    MoreCustomerViewController *more=[[MoreCustomerViewController alloc]initWIthCustomerStatus:@"0" searchKey:nameSearchBar.text communityId:communityIdStr];
                    [self.navigationController pushViewController:more animated:YES];
                }
                else
                {
                    [PromptInfoView showMessage:NSLocalizedString(@"SearchKey", nil)];
                }
                return;
            }
            else if(indexPath.row < normalArray.count)
            {
                GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
                GuestModel *model=[[GuestModel alloc]init];
                customSearchModel * cModel = normalArray[indexPath.row];
                model.guestName=cModel.name;
                model.telePhone=cModel.mobile;
                model.email=cModel.email;
                model.guestid=cModel.userId;
                model.headUrl=cModel.headurl;
                model.sex=cModel.gender;
                model.certificaties=cModel.identity;
                model.firstChar=cModel.firstChar;
                model.areaCode = cModel.areaCode;
                [QRSingleton sharedQRSingleton].guestPushIndex = 2;
                gmvc.model = model;
                gmvc.communityid = communityIdStr;
                gmvc.comeFrom = NO;
                [self.navigationController pushViewController:gmvc animated:YES];
            }
        }
        else if(indexPath.row < normalArray.count)
        {
            GuestManagerViewController * gmvc = [[GuestManagerViewController alloc]init];
            GuestModel *model=[[GuestModel alloc]init];
            customSearchModel * cModel = normalArray[indexPath.row];
            model.guestName=cModel.name;
            model.telePhone=cModel.mobile;
            model.email=cModel.email;
            model.guestid=cModel.userId;
            model.headUrl=cModel.headurl;
            model.sex=cModel.gender;
            model.certificaties=cModel.identity;
            model.firstChar=cModel.firstChar;
            model.areaCode = cModel.areaCode;
            [QRSingleton sharedQRSingleton].guestPushIndex = 2;
            gmvc.model = model;
            gmvc.communityid = communityIdStr;
            gmvc.comeFrom = NO;
            [self.navigationController pushViewController:gmvc animated:YES];
        }
    }

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if(groupArray.count!=0 && normalArray.count!=0)
        {
            if (indexPath.section==0) {
                if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
                {
                    if(indexPath.row== groupArray.count)
                    {
     
                    }
                    else if(indexPath.row < groupArray.count)
                    {
                        customSearchModel * model = [[customSearchModel alloc]init];
                        model=[groupArray objectAtIndex:indexPath.row];
                        groupIdStr=model.groupId;
                        clientIdStr=model.userId;
                        [self delete:@"1"];
                    }
                }
                else if(indexPath.row < groupArray.count)
                {
                    customSearchModel * model = [[customSearchModel alloc]init];
                    model=[groupArray objectAtIndex:indexPath.row];
                    groupIdStr=model.groupId;
                    clientIdStr=model.userId;
                    [self delete:@"1"];
                }
            }
            else if (indexPath.section==1) {
                if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
                {
                    if(indexPath.row== normalArray.count)
                    {
                       
                    }
                    else if(indexPath.row < normalArray.count)
                    {
                        customSearchModel * model = [[customSearchModel alloc]init];
                        model=[normalArray objectAtIndex:indexPath.row];
                        memberIdsStr=model.userId;
                        [self delete:@"0"];
                    }
                }
                else if(indexPath.row < normalArray.count)
                {
                    customSearchModel * model = [[customSearchModel alloc]init];
                    model=[normalArray objectAtIndex:indexPath.row];
                    memberIdsStr=model.userId;
                    [self delete:@"0"];
                }
                
            }
        }
        else if(groupArray.count!=0 && normalArray.count==0)
        {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== groupArray.count)
                {
                    
                }
                else if(indexPath.row < groupArray.count)
                {
                    customSearchModel * model = [[customSearchModel alloc]init];
                    model=[groupArray objectAtIndex:indexPath.row];
                    groupIdStr=model.groupId;
                    clientIdStr=model.userId;
                    [self delete:@"1"];
                }
            }
            else if(indexPath.row < groupArray.count)
            {
                customSearchModel * model = [[customSearchModel alloc]init];
                model=[groupArray objectAtIndex:indexPath.row];
                groupIdStr=model.groupId;
                clientIdStr=model.userId;
                [self delete:@"1"];
            }
        }
        else if(groupArray.count==0 && normalArray.count!=0)
        {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if(indexPath.row== normalArray.count)
                {
                    
                }
                else if(indexPath.row < normalArray.count)
                {
                    customSearchModel * model = [[customSearchModel alloc]init];
                    model=[normalArray objectAtIndex:indexPath.row];
                    memberIdsStr=model.userId;
                    [self delete:@"0"];
                }
            }
            else if(indexPath.row < normalArray.count)
            {
                customSearchModel * model = [[customSearchModel alloc]init];
                model=[normalArray objectAtIndex:indexPath.row];
                memberIdsStr=model.userId;
                [self delete:@"0"];
            }
            
        }
    }
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCellEditingStyle editStyle;
    if(groupArray.count!=0 && normalArray.count!=0)
    {
        if(indexPath.section==0)
        {
            if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if (indexPath.row == groupArray.count) {
                    editStyle=UITableViewCellEditingStyleNone;
                }
                else if(groupArray.count > indexPath.row)
                {
                   editStyle=UITableViewCellEditingStyleDelete;
                }
            }
            else if(groupArray.count > indexPath.row)
            {
               editStyle=UITableViewCellEditingStyleDelete;
            }
        }
        if(indexPath.section==1)
        {
            if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
            {
                if (indexPath.row ==normalArray.count) {
                  editStyle=UITableViewCellEditingStyleNone;
                }
                else if(normalArray.count > indexPath.row)
                {
                   editStyle=UITableViewCellEditingStyleDelete;
                }
                
            }
            else if(normalArray.count > indexPath.row)
            {
                  editStyle=UITableViewCellEditingStyleDelete;
            }
        }
    }
    else if(groupArray.count!=0 && normalArray.count==0)
    {
        if([groupPageDic objectForKey:@"totalcustomer"]!=nil && [[groupPageDic objectForKey:@"totalcustomer"] intValue]>4)
        {
            if (indexPath.row  == groupArray.count) {
               editStyle=UITableViewCellEditingStyleNone;
            }
            else if(groupArray.count > indexPath.row)
            {
                editStyle=UITableViewCellEditingStyleDelete;
            }
        }
        else if(groupArray.count > indexPath.row)
        {
               editStyle=UITableViewCellEditingStyleDelete;
        }
    }
    else if(groupArray.count==0 && normalArray.count!=0 )
    {
        if([normalPageDic objectForKey:@"totalcustomer"]!=nil && [[normalPageDic objectForKey:@"totalcustomer"] intValue]>4)
        {
            if (indexPath.row == normalArray.count) {
               editStyle=UITableViewCellEditingStyleNone;
                
            }
            else if(normalArray.count > indexPath.row)
            {
               editStyle=UITableViewCellEditingStyleDelete;
            }
        }
        else if(normalArray.count > indexPath.row)
        {
            editStyle=UITableViewCellEditingStyleDelete;
        }
    }

    return editStyle;
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  @"删除";
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    

}

-(void)delete:(NSString *)type//0表示普通，1表示组
{
    if ([@"0" isEqualToString:type]==YES) {
        [self deleteCustomers:communityIdStr memberIds:memberIdsStr];
    }
    else if ([@"1" isEqualToString:type]==YES)
    {
        [self deleteGroup:communityIdStr groupId:groupIdStr clientId:clientIdStr];
    }
}

//删除普通客户
-(void)deleteCustomers:(NSString *)communityId memberIds:(NSString *)memberIds
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [_activityIndicator startAnimating];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:communityId forKey:@"communityId"];
    [postDict setValue:memberIds forKey:@"memberIds"];
    
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(DELETECUSTOMERS) params:postDict block:^(id downLoadData, id error)
    {
        if (downLoadData) {
            [self getCustomerInfo:communityIdStr search:nameSearchBar.text currentPage:nil size:nil status:@"0,1"];
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        [_activityIndicator stopAnimating];
    }];
    
}

//删除组客户
-(void)deleteGroup:(NSString *)communityId groupId:(NSString *)groupId clientId:(NSString *)clientId
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [_activityIndicator startAnimating];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:communityId forKey:@"communityId"];
    [postDict setValue:groupId forKey:@"groupId"];
    [postDict setValue:clientId forKey:@"clientId"];
    RequestNetWork * rnw = [[RequestNetWork alloc]init];
    [rnw initWithPath:POST(DELETEGROUPCUSTOMER) params:postDict block:^(id downLoadData, id error)
    {
        if (downLoadData) {
            [self getCustomerInfo:communityIdStr search:nameSearchBar.text currentPage:nil size:nil status:@"0,1"];
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        [_activityIndicator stopAnimating];
    }];
}

//删除多余表格
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    
}


-(void)hiddenKeyboard
{
    [nameSearchBar resignFirstResponder];
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    nameSearchBar.delegate = nil;
    searchResultTableView.delegate = nil;
    
}

@end
